﻿using com.guojin.mj.net;
using com.guojin.mj.net.message.login;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text;
using System;
using com.guojin.mj.net.message.game;
public class MyClubbPanel : EventManager {
    public GameObject view,createpanel,joinPanel;
    public Button create, join,TiJiaoCreate,createRet,JoinRet,JoinBut,head;
    public Button JoinLM,CloseLM,JoinLMOK,ShowLm;
    public InputField clubnam,JoinID,JoinLMid;
    public int MyClubId;
    public string HImage="a";
    public override void InformationSetting()
    {
       
    }
    public void SetMyClubbPanel(ClubMessageRet cb) {
        if (cb.Clubs!=null)
        {
            view.transform.GetComponentsInChildren<OneP.InfinityScrollView.InfinityScrollView>()[0].Setup(cb.Clubs.Count);
        }
    }
    private void Awake()
    {
        BindingSource[0].GetComponent<Button>().onClick.AddListener(delegate
        {
            FaXianBtnClick();
        });
        BindingSource[2].GetComponent<Button>().onClick.AddListener(delegate
        {
            FirstPanel();
            Des();
        });
        BindingSource[3].GetComponent<Button>().onClick.AddListener(delegate
        {
            ZhanJiBtnClick();
        });
        BindingSource[4].GetComponent<Button>().onClick.AddListener(delegate
        {
            GetMe();
            Des();
        });
    }
    private void Start()
    {
        AddListenerToButton(create, CreateClubs);
        AddListenerToButton(createRet, CreateClubRet);
        AddListenerToButton(join, OnJoin);
        AddListenerToButton(JoinRet, OnJoinRet);
        AddListenerToButton(TiJiaoCreate, CreateMyClub);
        AddListenerToButton(JoinBut, JoinGo);
        AddListenerToButton(JoinLM, ShowJoinLM);
        AddListenerToButton(CloseLM, CloseJoinLM); 
        AddListenerToButton(JoinLMOK, JoinLmOK);
        AddListenerToButton(ShowLm, ShowLM);
        AddListenerToButton(head, ChageImage);
    }
    void Des()
    {
        Destroy(this.gameObject);
    }
    void FaXianBtnClick()
    {
        MessageManager.GetInstance().Send7140();
    }
    void ZhanJiBtnClick()
    {
        MessageManager.GetInstance().Send7131(0, GameData.GetInstance().userID);
    }
    void CreateClubs() {
        createpanel.gameObject.SetActive(true);
    }
    void CreateMyClub()
    {
        Debug.Log("创建俱乐部");
        if (clubnam.text!=null&& clubnam.text != "")
        {
#if UNITY_EDITOR
            MessageManager.GetInstance().SendMsg771("", clubnam.text);
#else
            if (HImage != null && HImage != "a" && HImage !="")
            {
                string param = "{ \"type\":\"img\",\"data\":\"data:image/png;base64," + HImage + "\"}";
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("data", param);
                string result = HttpPostData(ServerAdress, dic);
                //服务器返回语音地址
                Debug.Log("服务器返回地址:" + result);
                MessageManager.GetInstance().SendMsg771(result, clubnam.text);
            }
            else
            {
                TooL.WenXinTiSi("您没有选择头像");
            }
#endif
        }
        else
        {
            TooL.WenXinTiSi("您的输入有误，请重新输入");
        }
    }
    void CreateClubRet()
    {
        createpanel.gameObject.SetActive(false);
    }
    void TiJiaoMsg() {
        if (clubnam.text!=null&&clubnam.text!="")
        {
#if UNITY_EDITOR
            MessageManager.GetInstance().SendMsg771("", clubnam.text);
#else
            if (HImage != null && HImage != "a" && HImage !="")
            {
                string param = "{ \"type\":\"img\",\"data\":\"data:image/png;base64," + HImage + "\"}";
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("data", param);
                string result = HttpPostData(ServerAdress, dic);
                //服务器返回语音地址
                Debug.Log("服务器返回地址:" + result);
                MessageManager.GetInstance().SendMsg771(result, clubnam.text);
            }
            else
            {
                TooL.WenXinTiSi("您没有选择头像");
            }
#endif

        }
        else
        {
            TooL.WenXinTiSi("您输入的格式有误，请检查后重新输入");
        }      
    }
    void OnJoinRet() {
        joinPanel.SetActive(false);
    }
    void OnJoin()
    {
        joinPanel.SetActive(true);
    }
    void JoinGo() {
        if (JoinID.text != null && JoinID.text != "")
        {
            MessageManager.GetInstance().SendMsg784(int.Parse(JoinID.text));
        }
        else
        {
            TooL.WenXinTiSi("您输入的格式有误，请检查后重新输入");
        }
    }
    void FirstPanel()
    {
        UIManager.ChangeUI(UIManager.PageState.GameHallPage, (GameObject obj) =>
        {
            obj.GetComponent<GameHallPanel>().InformationSetting();
        });
    }
    void GetMe()
    {
        UIManager.ChangeUI(UIManager.PageState.GetMe, (GameObject obj) =>
        {
            obj.GetComponent<GetMePanel>().InformationSetting();
        });
    }
    void ShowJoinLM() {
        BindingSource[5].SetActive(true);
    }
    void CloseJoinLM()
    {
        BindingSource[5].SetActive(false);
    }
    void JoinLmOK()
    {
        if (JoinLMid.text != null && JoinLMid.text != "")
        {
            MessageManager.GetInstance().SendMsg7107(MyClubId,int.Parse(JoinLMid.text));
        }
        else
        {
            TooL.WenXinTiSi("您输入的格式有误，请检查后重新输入");
        }
    }
    void ShowLM()
    {
        MessageManager.GetInstance().Send7101();
    }
    void ChageImage() {
        LoginAndShare.Controller.OpenPhoto(0);
    }
    private string ServerAdress = "http://121.40.90.67:8092/up";
    //这个是收到1.25   4类型代表语音
    public int msgType = 4;

    //发送录音文件字节流，将返回字节流广播出去
    public void SendName()
    {
        //if (Name.text == null || Name.text == "")
        //{
        //    TooL.WenXinTiSi("昵称不能为空");
        //    return;
        //}
        //if (headsBase64 == "a")
        //{
        //    TooL.WenXinTiSi("头像不能为空");
        //    return;
        //}
        //string param = "{ \"type\":\"img\",\"data\":\"data:image/png;base64," + headsBase64 + "\"}";
        //Dictionary<string, string> dic = new Dictionary<string, string>();
        //dic.Add("data", param);
        //string result = HttpPostData(ServerAdress, dic);
        ////服务器返回语音地址
        //Debug.Log("服务器返回地址:" + result);
        //MessageManager.GetInstance().Send7148(result, Name.text, sex, acct, pass);
    }
    //发送录音字节流 收到服务器返回网址
    public string HttpPostData(string url, Dictionary<string, string> dic)
    {
        var result = string.Empty;
        //#region 添加Post 参数  
        StringBuilder builder = new StringBuilder();
        int i = 0;
        foreach (var item in dic)
        {
            if (i > 0)
                builder.Append("&");
            builder.AppendFormat("{0}={1}", item.Key, item.Value);
            i++;
        }

        byte[] postData = Encoding.UTF8.GetBytes(builder.ToString());
        // 设置提交的相关参数 

        System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
        Encoding myEncoding = Encoding.UTF8;
        request.Method = "POST";
        request.KeepAlive = false;
        request.AllowAutoRedirect = true;
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        request.ContentLength = postData.Length;
        request.Timeout = 3000;

        // 提交请求数据 
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();

        System.Net.HttpWebResponse response;
        Stream responseStream;
        StreamReader reader;
        string srcString;
        response = request.GetResponse() as System.Net.HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
        srcString = reader.ReadToEnd();
        result = srcString;   //返回值赋值
        reader.Close();
        Debug.Log("获取HttpPostData服务器返回Version" + result);
        return result;
    }
}
